package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        //调用业务层查询到所有留言信息的方法
        GuestbookService service=new GuestbookServiceImpl();
        //将数据保存到list集合中
        List<Object> list=service.queryInfo();
        writer.println(list.size());
        writer.println("<html>");
        writer.println("<head><title>获取所有信息</title></head>");
        writer.println("<body>");
        writer.println("<table border='1px '>");
        writer.println("<tr>");
        writer.println("<td>id</td>");
        writer.println("<td>name</td>");
        writer.println("<td>phone</td>");
        writer.println("<td>email</td>");
        writer.println("<td>title</td>");
        writer.println("<td>content</td>");
        writer.println("<td>createdtime</td>");
        writer.println("</tr>");

        for (int i=0;i<list.size();i++){
            //从集合中获取guestbook对象
            Guestbook gb= (Guestbook) list.get(i);
            writer.println("<tr>");
            writer.println("<td>"+gb.getId()+"</td>");
            writer.println("<td>"+gb.getName()+"</td>");
            writer.println("<td>"+gb.getPhone()+"</td>");
            writer.println("<td>"+gb.getEmail()+"</td>");
            writer.println("<td>"+gb.getTitle()+"</td>");
            writer.println("<td>"+gb.getContent()+"</td>");
            writer.println("<td>"+gb.getCreatedtime()+"</td>");
            writer.println("</tr>");
        }
        writer.println("</table>");
        writer.println("</body>");
        writer.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
